package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.manager.service.IPlateService;
import com.codedancer.campus.manager.service.bo.plate.PlateAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.plate.PlatePageSearchBo;
import com.codedancer.campus.manager.service.vo.plate.PlatePageVo;
import com.codedancer.campus.manager.service.vo.plate.PlateVo;
import com.codedancer.campus.mapper.CategoryMapper;
import com.codedancer.campus.mapper.CommodityMapper;
import com.codedancer.campus.mapper.PlateMapper;
import com.codedancer.campus.po.Plate;
import com.codedancer.campus.qo.plate.PlatePageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class PlateServiceImpl implements IPlateService {

    @Autowired
    private PlateMapper plateMapper;
    @Autowired
    private CommodityMapper commodityMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 分页查询板块
     *
     * @param platePageSearchBo
     * @return
     */
    @Override
    public IPage<PlatePageVo> page(PlatePageSearchBo platePageSearchBo) {
        PlatePageSearchQo platePageSearchQo = BeanUtil.copyProperties(platePageSearchBo,
            PlatePageSearchQo.class);
        IPage<Plate> plateIPage = plateMapper.page2Manager(
            new Page<Plate>(platePageSearchBo.getPage(), platePageSearchBo.getSize()),
            platePageSearchQo);
        IPage<PlatePageVo> platePageVoIPage = new Page<>();
        List<PlatePageVo> platePageVos;
        platePageVos = plateIPage.getRecords().stream()
            .map(plate -> BeanUtil.copyProperties(plate, PlatePageVo.class)).collect(
                Collectors.toList());
        platePageVoIPage.setRecords(platePageVos);
        platePageVoIPage.setTotal(plateIPage.getTotal());
        return platePageVoIPage;
    }

    /**
     * 删除板块
     *
     * @param uuid
     */
    @Override
    public void delete(String uuid) {
        Plate plate = plateMapper.findById(uuid).orElseThrow(() -> new BusinessException("板块不存在"));
        if (commodityMapper.existsByColumn("plate_uuid", uuid)) {
            throw new BusinessException("该板块已被使用，不能删除");
        }
        // 先删除该板块下的分类
        categoryMapper.deleteByColumn("plate_uuid", uuid);
        // 删除板块
        plateMapper.deleteById(uuid);
    }

    /**
     * 添加板块
     *
     * @param plateAddOrUpdateBo
     */
    @Override
    public void add(PlateAddOrUpdateBo plateAddOrUpdateBo) {
        if (plateMapper.existsByColumn("code", plateAddOrUpdateBo.getCode())) {
            throw new BusinessException("该板块编码已存在，不能添加");
        }
        Plate plate = BeanUtil.copyProperties(plateAddOrUpdateBo, Plate.class);
        plate.setUuid(UUIDGenerator.getUUID());
        plateMapper.insert(plate);
    }

    /**
     * 修改板块
     *
     * @param plateAddOrUpdateBo
     */
    @Override
    public void update(PlateAddOrUpdateBo plateAddOrUpdateBo) {
        Plate plate = plateMapper.findById(plateAddOrUpdateBo.getUuid())
            .orElseThrow(() -> new BusinessException("板块不存在"));
        if (!plate.getCode().equals(plateAddOrUpdateBo.getCode())) {
            // 板块编码不一致
            if (plateMapper.existsByColumn("code", plateAddOrUpdateBo.getCode())) {
                throw new BusinessException("该板块编码已存在，不能添加");
            }
            plate.setCode(plateAddOrUpdateBo.getCode());
        }
        plate.setName(plateAddOrUpdateBo.getName());
        plate.setRemark(plateAddOrUpdateBo.getRemark());
        plateMapper.updateById(plate);
    }

    /**
     * 获取全部板块
     *
     * @return
     */
    @Override
    public List<PlateVo> all() {
        List<Plate> plates = plateMapper.selectList(null);
        return plates.stream().map(plate -> BeanUtil.copyProperties(plate, PlateVo.class)).collect(
            Collectors.toList());
    }
}
